package com.xinqing.bigdata.flink.datastream.operators;

import com.xinqing.bigdata.flink.datastream.model.Worker;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Properties;

/**
 * @Author:CHQ
 * @Date:2021/5/20 16:36
 * @Description
 */
public class Reduce {
    public static void main(String args[]) throws Exception {

        //env,port
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(new Configuration());

        //checkpoint conf
        CheckpointConfig checkpointConfig = env.getCheckpointConfig();
        checkpointConfig.setMaxConcurrentCheckpoints(1);
        checkpointConfig.setCheckpointTimeout(60000);
        checkpointConfig.setCheckpointInterval(60000);
        checkpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

        //Kafka conf
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "10.201.7.175:9092");
        properties.setProperty("group.id", "chq9");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        // properties.setProperty("auto.offset.reset", "earliest");
        properties.setProperty("auto.offset.reset", "latest");

        ArrayList<Worker> list = new ArrayList<>();
        list.add(new Worker("1", "chq", "23"));
        list.add(new Worker("2", "cl", "66"));
        list.add(new Worker("2", "xx", "20"));
        list.add(new Worker("2", "yy", "30"));
        list.add(new Worker("3", "zjl", "46"));


        //reduce->将处理相邻的两个元素处理成一个元素后返回，并以此为baseline，继续处理最近输入的一个元素，以此类推
        env.fromCollection(list)
                .keyBy((KeySelector<Worker, Object>) value -> value.getNum())
                .reduce((ReduceFunction<Worker>) (value1, value2) ->
                        new Worker("4", "x", Integer.toString(Integer.parseInt(value1.getAge()) + Integer.parseInt(value2.getAge()))))
                .setParallelism(4).print();

        env.execute();

    }
}
